package com.lly.songhigh.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.activity.SingerDetailActivity;
import com.lly.songhigh.adapter.SearchSingerListViewAdapter;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.SearchSinger;
import com.lly.songhigh.utils.SharedPreferencesUtils;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by dllo on 15/10/24.
 */
public class SearchSingerFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView singerListView;
    private CircleImageView circleImageView;
    private String startUrl = URL.SEARCHSINGER;
    private String endUrl;
    private List<SearchSinger.DataEntity> datas;
    private SearchSinger.DataEntity data;
    private VolleySingleton volleySingleton;
    private String url;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_singer, null);
//        ButterKnife.bind(this, view);
        singerListView = (ListView) view.findViewById(R.id.singer_listView);
        singerListView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getInstance();
        endUrl = SharedPreferencesUtils.getSharePreStr(getActivity(), "SearchActivityEditText", "endUrl");
        url = startUrl + endUrl;
        initData();
        return view;
    }

    private void initData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Gson gson = new Gson();
                SearchSinger searchSinger = gson.fromJson(response, SearchSinger.class);
                datas = searchSinger.getData();
                SearchSingerListViewAdapter searchSingerListViewAdapter = new
                        SearchSingerListViewAdapter(getActivity(), datas);
                singerListView.setAdapter(searchSingerListViewAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(getActivity(), SingerDetailActivity.class);
        data =datas.get(position);
        intent.putExtra("singerId", data.get_id());
        startActivity(intent);
    }
}
